package com.lz.football.module.activity.vip
{
	import com.common.utils.Reflection;
	import com.common.utils.SharedEnterFrame;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.liangzhong.events.ActivityEvent;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.activity.vip.VipAwardVo;
	import com.liangzhong.model.activity.vip.VipVo;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.lz.football.module.activity.IVipWard;
	import com.lz.football.module.bag.item.BagItem;
	import com.lz.football.module.base.IComponent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import lz.controls.CompGlobalDef;
	import lz.controls.managers.AnimationManager;

	/**
	 *vip每日奖励动画面板 
	 * @author Administrator
	 * 
	 */	
	public class VipDailyWardView extends Sprite implements IComponent,IVipWard
	{
		private var _skin:MovieClip;
		
		private var getwardBtn:SimpleButton;
		/**
		 * 
		 */
		private var awardContainer:MovieClip;
		/**
		 * 
		 */
		private var taskFinishBg:MovieClip;
		/**
		 * 物品动画 
		 */
		private var _vecMc:Array
		/**
		 *物品 
		 */		
		private var _goodsMc:Vector.<MovieClip>;
		/**
		 *经验，欧元 
		 */
		private var _b_mc:Vector.<MovieClip>;
		
		/**
		 *是否请求领取 
		 */
		private var _isRequest:Boolean;
		private var _obj:Object;
		public function VipDailyWardView()
		{
			super();
		}
		
		public function initUI(view:Object=null):void
		{
			_skin = Reflection.createInstance("com.lz.res.activity.vipdailymc");
			if(_skin ==  null)
			{
				SharedEnterFrame.instance.addFrameCall(refresh,1);
				return;
			}
			this.addChild(_skin);
			getwardBtn = _skin.getwardBtn;
			awardContainer = _skin.awardContainer;
			taskFinishBg = _skin.vipwardbg
			addEvtListener();
			onResizeHandler();
		}
		private function refresh():void
		{
			_skin = Reflection.createInstance("com.lz.res.activity.vipdailymc");
			if(_skin ==  null)
			{
				return;
			}
			this.addChild(_skin);
			getwardBtn = _skin.getwardBtn;
			awardContainer = _skin.awardContainer;
			taskFinishBg = _skin.vipwardbg
			addEvtListener();
			onResizeHandler();
			showWard(_obj);
			SharedEnterFrame.instance.removeFrameCall(refresh);
		}
		public function onResizeHandler(event:Event = null):void
		{
			this.x = (GlobalDef.StageWidth - 862)>> 1;
			this.y = (GlobalDef.StageHeight - 250)>> 1;
		}	
		public function addEvtListener():void
		{
			GlobalDef.GameStage.addEventListener(Event.RESIZE,onResizeHandler);
			getwardBtn.addEventListener(MouseEvent.CLICK,onGetWardHandler);
		}
		
		protected function onGetWardHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			getwardBtn.removeEventListener(MouseEvent.CLICK,onGetWardHandler);
			if(_isRequest)
				AppEventDispatcher.getInstance().dispatchEvent(new ActivityEvent(ActivityEvent.ACTIVITY_VIP_DAILY_AWARD,{}));
			getWard();
			
			
		}
		
		public function removeEvtListener():void
		{
			GlobalDef.GameStage.removeEventListener(Event.RESIZE,onResizeHandler);
			getwardBtn.removeEventListener(MouseEvent.CLICK,onGetWardHandler);
		}
		
		public function destroyView():void
		{
			removeEvtListener();
			if(this.parent)this.parent.removeChild(this);
		}
		/**
		 *vip等级 
		 * @param $lv
		 * 
		 */
		public function showWard($obj:Object):void
		{
			_obj = $obj;
			if(_skin == null)return;
			if(_obj == null)return;
			var $lv:int = $obj.lv;
			_isRequest = $obj.isRequest;
			_vecMc = new Array();
			_goodsMc = new Vector.<MovieClip>();
			_skin.dec.text = "领取VIP"+$lv+"的每日奖励";
			var vo:VipVo = AppData.getInstance().activityDatemanager.getVipVoByVipLv($lv);
			if(vo == null)return;
			var arr:Array = vo.daily;
			var len:int = arr.length;
			var bagItem:BagItem;
			var goods:GoodsVo;
			var vipAwardVo:VipAwardVo;
			var tf:TextField;
			var mc:MovieClip //= Reflection.createInstance("com.lz.modul.task.goodsItem");
			for(var i:int = 0;i < len;i++)
			{
				mc = Reflection.createInstance("com.lz.modul.task.goodsItem");
				vipAwardVo = arr[i];
				goods = vipAwardVo.goods;
				mc.gotoAndStop(1);
				bagItem = new BagItem();
				bagItem.initUI();
				bagItem.data = goods;
				bagItem.removeEvtListener();
				mc.x = 75 * Math.ceil(awardContainer.numChildren/2);
				
				mc.mc.contain.addChild(bagItem);
				awardContainer.addChild(mc);
				mc.visible = false;
				tf = createText();
				tf.text = "" + goods.name + "*" +vipAwardVo.num ;
				tf.x = mc.x- 15;
				_vecMc.push([mc,tf]);
				_goodsMc.push(mc);
			}
			_skin.addFrameScript(1,onsetAlpha);
			play();
		}
		/**
		 * 开始物品奖励动画  
		 * 
		 */		
		private function play():void
		{
			var mc:MovieClip;
			var arr:Array
			if(_vecMc.length > 0)
			{
				arr =  _vecMc.shift()
				mc =arr[0];
				mc.gotoAndPlay(1);
				mc.addFrameScript(4,play);
				mc.visible = true;
				arr[1].visible = true;
			}
		}
		/**
		 * 设置位置 
		 * 
		 */		
		private function onsetAlpha():void
		{
			awardContainer.visible = true;
			awardContainer.alpha = 1;
			onResizeHandler();
			awardContainer.x = (taskFinishBg.width - awardContainer.width) >> 1;
		}
		/**
		 * 创建文本 
		 * @return 
		 * 
		 */		
		private function createText():TextField
		{
			var tx:TextField = new TextField();
			tx.height = 20;
			tx.width = 90;
			tx.textColor = 0xFFFFFF;
			tx.mouseEnabled = false;
			tx.text = "---";
			tx.y = 60;
			var tf:TextFormat =new TextFormat();
			tf.align = TextFieldAutoSize.CENTER;
			tx.defaultTextFormat = tf;
			tx.text = "---";
			awardContainer.addChild(tx);
			tx.visible = false;
			return tx;
		}
		/**
		 *领取奖励返回 
		 * 
		 */
		public function getWard():void
		{
			var i:int;
			var point:Point
			if(_goodsMc.length > 0)
			{
				for(i = 0;i < _goodsMc.length;i++)
				{
					point = _goodsMc[i].localToGlobal(new Point(0,0));
					GlobalDef.AlertContainer.addChild(_goodsMc[i]);
					_goodsMc[i].x = point.x;
					_goodsMc[i].y = point.y;
					TweenLite.to(_goodsMc[i],1,{x:CompGlobalDef.BagPoint.x + 25,y:CompGlobalDef.BagPoint.y + 25,scaleX:0,scaleY:0,onComplete:onComplete,onCompleteParams:[_goodsMc[i]]});
				}
			}
			destroyView();
			AnimationManager.remove();
			
		}
		private function onComplete(mc:MovieClip):void
		{
			if(mc.parent)
				mc.parent.removeChild(mc);
		}
	}
}